package com.tanhua.app.controller;

import com.tanhua.app.service.CommentService;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * Get /comments
     * 查询评论，按照分页进行查询
     * @return
     */
    @GetMapping
    public ResponseEntity findComments(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       String movementId){
        //1.调用Service执行业务逻辑
        PageResult pageResult = commentService.findComments(page,pagesize,movementId);

        //2.返回结果
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 发布评论
     * @param param
     * @return
     */
    @PostMapping
    public ResponseEntity saveComments(@RequestBody Map param){
        //1.获取请求参数
        String movementId = (String) param.get("movementId");
        String comment = (String) param.get("comment");

        //2.调用Service执行业务逻辑
        commentService.saveComments(movementId, comment);

        //3.返回结果
        return ResponseEntity.ok(null);
    }
}
